package com.loit.business.utils;


import com.loit.business.entity.ReqComVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 远程调用，post请求，传输表单类型的参数
 */
@Slf4j
public class HttpUtils {
 
 
   //JSON方式的调用 （这里入参我是选用了Map类型 然后通过JSON转了一下，非常人性化）
//   public static Map post(String url, Map body) {
//
//      HttpPost httpPost = new HttpPost(url);
//      httpPost.addHeader("Content-Type", "application/json");
//      httpPost.addHeader("Accept", "application/json");
//      StringEntity entity = new StringEntity(Objects.requireNonNull(JSON.toJSONString(body)), "utf-8");
//      entity.setContentEncoding("UTF-8");
//      entity.setContentType("application/json");
//      httpPost.setEntity(entity);
//      return doExecute(httpPost);
//   }
 
 
   //form-data类型  表单类型参数  ReqComVO根据需求改动
   public static String postTst(String url, ReqComVO comVO){
      HttpPost httpPost = new HttpPost(url);
      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
 
      //注意这里 首先我这边没有设置http请求头 如果不加contenType的话 会出现参数乱码
      ContentType contentType = ContentType.create("text/plain", "UTF-8");
 
      //这里依然是使用addTextBoby的方式带有contentType的方法
//      builder.addTextBody("params",JSON.toJSONString(body),contentType);
      builder.addTextBody("method",comVO.getMethod(),contentType)
              .addTextBody("app_id",comVO.getApp_id(),contentType)
              .addTextBody("security",comVO.getSecurity(),contentType)
              .addTextBody("format",comVO.getFormat(),contentType)
              .addTextBody("datetime",comVO.getDatetime(),contentType)
              .addTextBody("version",comVO.getVersion(),contentType)
              .addTextBody("region_code",comVO.getRegion_code(),contentType)
              .addTextBody("agency_code",comVO.getAgency_code(),contentType)
              .addTextBody("encryption",comVO.getEncryption(),contentType)
              .addTextBody("message_id",comVO.getMessage_id(),contentType)
              .addTextBody("message",comVO.getMessage(),contentType);
      httpPost.setEntity(builder.build());
      return doExecute(httpPost);
   }
 
   private static String doExecute(HttpUriRequest request) {
      try {
         CloseableHttpClient httpClient = HttpClients.createDefault();
         HttpResponse httpResponse;
 
         httpResponse = httpClient.execute(request);
         int httpResponseCode = httpResponse.getStatusLine().getStatusCode();
         if (httpResponseCode != 200) {
            log.error("doExecute. httpResponseCode={}, request={}", httpResponseCode, request);
         }
         String resultString = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
         return resultString;
      } catch (Exception e) {
         log.error("doExecute error:", e);
         return null;
      }
   }
 
   private static String doExecuteTest(HttpUriRequest request) {
      try {
         CloseableHttpClient httpClient = HttpClients.createDefault();
         HttpResponse httpResponse;
 
         httpResponse = httpClient.execute(request);
         int httpResponseCode = httpResponse.getStatusLine().getStatusCode();
         if (httpResponseCode != 200) {
            log.error("doExecute. httpResponseCode={}, request={}", httpResponseCode, request);
         }
         return  EntityUtils.toString(httpResponse.getEntity(), "utf-8");
      } catch (Exception e) {
         log.error("doExecute error:", e);
         return null;
      }
   }
 
 
//   public static void main(String[] args) {
//      List<Map> list =new ArrayList<>();
//      Map map = new HashMap();
//      map.put("name","赖勇强");
//      map.put("idcard","362532199308071714");
//      map.put("lanCode","360000");
//      map.put("countyCode","1");
//      map.put("bizType","8");
//      map.put("targetUrl","");
//      map.put("pushTemplateJson","{\"bizcode\":\"xkslwk\",\"jfyhxm\":\"赖勇强测试\",\"jfsj\":\"2020-12-21\",\"jfje\":\"100.2\"}");
//      list.add(map);
//      Map post = postTst("https://ganfutong.jiangxi.gov.cn/dztxcs/interfaces/dztxgeneral/dztxGeneralPushData.do", list);
//      System.out.println("post--->"+post);
//   }
}